/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.app.content;

import han.bifang.gwt.client.app.AppContentConstants;
import han.bifang.gwt.client.app.AppMessages;
import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Application;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

/**
 * 
 * @Author Kent
 */
public class ApplicationRootContent extends Content {
	
	private AppMessages cons = null;

	private BifangGWTServiceAsync service = null;

	
	private GwtContext context = null;
	
	private VerticalPanel panel = null;
	
	private FlexTable apps;
	
	private Button nb = null;
	
	public ApplicationRootContent(AppMessages cons,BifangGWTServiceAsync service,GwtContext context){
		this.cons = cons;
		this.service = service;
		this.context = context;
		panel = new VerticalPanel();
		panel.setSpacing(25);
		panel.setWidth("100%");
		
		
		VerticalPanel p1 = new VerticalPanel();
		p1.setSpacing(0);
		p1.setWidth("100%");
		
		HTML l = new HTML(cons.appList());
		apps = new FlexTable();
		apps.setWidth("80%");
		
		apps.setCellPadding(1);
		apps.setCellSpacing(1);
		apps.setBorderWidth(1);
		
		apps.setHTML(0, 0, cons.name());
		apps.setHTML(0, 1, cons.code());
		apps.setHTML(0, 2, cons.desc());
		
		ColumnFormatter cf = apps.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");
		
		RowFormatter rf = apps.getRowFormatter();
		rf.setStyleName(0, "tableheader");
		
		nb = new Button(cons.newEntity());
		nb.addClickListener(new ClickListener(){
			public void onClick(Widget sender){
				HashMap t = new HashMap();
				
				listener.onContentChanged(AppContentConstants.APP_ROOT,
						AppContentConstants.CREATE_APP, t);
				
			}
		});
		
		p1.add(l);
		p1.add(apps);
		if(PrivilegChecker.hasPrivilege(context,null))
			p1.add(nb);
		
		panel.add(p1);
		
		
		initWidget(panel);	
		
	}
	
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList ds = (ArrayList) res.getResponse();
			for (int i = 0; i < ds.size(); i++) {
				Application d = (Application)ds.get(i);
				
					int row = apps.getRowCount();
					apps.setHTML(row,0, d.getName());
					apps.setHTML(row,1, d.getCode());
					
					apps.setHTML(row,2, d.getDescription());
					
					
				
			}
			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};
	
	
	
	
	public void onLoad(){
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.app.GetAppAction");
		service.call(req, cb);
		
	}
	
	public void onUnload(){
		apps = null;
		nb = null;
	}
	

}
